package info.xiao.dataStructrue.array;

import java.util.Arrays;

/**
 * @Classname MinMoves
 * @Description 最小操作次数使数组元素相等
 * @Date 2023/3/9 15:13
 * @Created by 晓龙Oba
 */
public class MinMoves {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1000};
        int times = minMoves(nums);
        System.out.println(times);
    }

    private static int minMoves(int[] nums) {
        int times = 0;
        Arrays.sort(nums);
        // 判断
        if (nums.length == 1 || nums[nums.length - 1] == nums[0]) {
            return times;
        }
        // 开启循环
        for (int i = nums.length - 1; i >= 0; i--) {
            times += nums[i] - nums[0] > 0 ? nums[i] - nums[0] : 0;
        }
        return times;
    }
}
